//
//  BookCollectionCell.m
//  KidReading
//
//  Created by telen on 15/7/29.
//  Copyright (c) 2015年 刘赞黄Telen. All rights reserved.
//

#import "FuncCollectionCell.h"
#import "ConstDefine.h"
#import "MDRadialProgressTheme.h"
#import "MDRadialProgressView.h"

@interface FuncCollectionCell()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *defaultImageViewWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *coverLeftPod;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *coverRightPod;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *coverTopPod;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *flagImageViewWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *downDoneImgViewWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *downDoneImgViewRightPod;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *theNewImgviewWdith;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *theNewImgviewRightPod;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bookShadowBottom;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bookShadowRight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *titleLabelTopPod;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageDoneIconBottom;
//
@property (weak, nonatomic) IBOutlet UIButton *bigBtn;

@end

@implementation FuncCollectionCell
{
    UIView* gradCoverView;
    MDRadialProgressView *radialView;
}

- (IBAction)didTouchedCell:(id)sender {
    if (self.didTouchCell) {
        self.didTouchCell(self);
    }
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    CGFloat ratio = [DataProcessing shareSingle].ratio_w;
    _lable_title.textColor = [UIColor colorWithHexString:@"4f4f4f"];
    _lable_title.verticalAlignment = VerticalAlignmentTop;
    _defaultImageViewWidth.constant /= ratio;
    _coverLeftPod.constant /= ratio;
    _coverRightPod.constant /= ratio;
    _coverTopPod.constant /= ratio;
    _flagImageViewWidth.constant /= ratio;
    _downDoneImgViewWidth.constant /= ratio;
    _downDoneImgViewRightPod.constant /= ratio;
    _theNewImgviewRightPod.constant /= ratio;
    _theNewImgviewWdith.constant /= ratio;
    _bookShadowBottom.constant /= ratio;
    _bookShadowRight.constant /= ratio;
    _titleLabelTopPod.constant /= ratio;
    _imageDoneIconBottom.constant /= ratio;
    self.newflag_imgv.hidden = YES;
    self.imagview_downDoneIcon.hidden = YES;
    //
    [self.bigBtn addTouchScaleTransitions_forView:self];
}

- (void)setDownProgress:(NSInteger)numOf100
{
    CGFloat w = self.view_bookcover.bounds.size.width;
    CGFloat h = self.view_bookcover.bounds.size.height;
    //
    self.imagview_downDoneIcon.hidden = YES;
    //
    //变态的使用 borderWidth 只是因为在某个设备上，gradCoverView没有被绘制
    if (!gradCoverView) {
        UIColor * bgClr = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        gradCoverView = [[UIView alloc] initWithFrame:self.view_bookcover.bounds];
        [self.view_bookcover addSubview:gradCoverView];
        UIView* bg = [[UIView alloc] initWithFrame:gradCoverView.bounds];
        [gradCoverView addSubview:bg];
        //bg.layer.borderWidth = h*0.5;
        //bg.layer.borderColor = bgClr.CGColor;
        gradCoverView.backgroundColor = bgClr;
        //
        CGRect frame = CGRectMake(0, 0, ceill(w*0.65*0.5)*2, ceill(w*0.65*0.5)*2);
        UIView* view = [[UIView alloc] initWithFrame:frame];
        [gradCoverView addSubview:view];
        view.center = CGPointMake(w/2, h/2);
        view.backgroundColor = [UIColor whiteColor];
        view.alpha = 0.8;
        [view.layer setCornerRadius:frame.size.width/2];
        [view.layer setMasksToBounds:YES];
        //
        //view.layer.borderWidth = frame.size.width*0.5;
        //view.layer.borderColor = [UIColor whiteColor].CGColor;
        //
        [gradCoverView setNeedsDisplay];
    }
    if (!radialView) {
        //
        MDRadialProgressTheme *newTheme = [[MDRadialProgressTheme alloc] init];
        newTheme.completedColor = [UIColor colorWithRed:86/255.0 green:103/255.0 blue:198/255.0 alpha:1.0];
        newTheme.incompletedColor = [UIColor colorWithRed:126/255.0 green:143/255.0 blue:218/255.0 alpha:0.2];
        newTheme.centerColor = [UIColor colorWithRed:126/255.0 green:143/255.0 blue:218/255.0 alpha:0.2];
        newTheme.sliceDividerHidden = YES;
        newTheme.labelColor = [UIColor colorWithRed:86/255.0 green:103/255.0 blue:198/255.0 alpha:1.0];
        newTheme.labelShadowColor = [UIColor whiteColor];
        newTheme.font = [UIFont boldSystemFontOfSize:w*0.12];
        newTheme.thickness = 6;
        //
        CGRect frame = CGRectMake(0, 0, ceill(w*0.6*0.5)*2, ceill(w*0.6*0.5)*2);
        radialView =  [[MDRadialProgressView alloc] initWithFrame:frame andTheme:newTheme];
        radialView.center = CGPointMake(w/2, h/2);
        radialView.endString = @"%";
        [self.view_bookcover addSubview:radialView];
        radialView.progressTotal = 100;
    }
    radialView.progressCounter = numOf100;
}

- (void)updateDownProgress:(NSInteger)numOf100
{
    [DataProcessing do_Main_quick:^{
        [self setDownProgress:numOf100];
        _status = FuncCellStatus_downIng;
    }];
}

- (void)setBookDownStart
{
    [DataProcessing do_Main_quick:^{
        [self setDownProgress:2015];
        _status = FuncCellStatus_downStart;
    }];
}

- (void)setBookDownDone
{
    _status = FuncCellStatus_downDone;
    if (gradCoverView) {
        [gradCoverView removeFromSuperview];
        gradCoverView = nil;
    }
    if (radialView) {
        [radialView removeFromSuperview];
        radialView = nil;
    }
    self.imagview_downDoneIcon.hidden = NO;
}

- (void)setBookToDelete
{
    _status = FuncCellStatus_toDelete;
}

- (void)setBookDownFail
{
    _status = FuncCellStatus_downFail;
    if (gradCoverView) {
        [gradCoverView removeFromSuperview];
        gradCoverView = nil;
    }
    if (radialView) {
        [radialView removeFromSuperview];
        radialView = nil;
    }
    self.imagview_downDoneIcon.hidden = YES;
}

- (void)setBookDownNone
{
    [self setBookDownFail];
    _status = FuncCellStatus_none;
}

@end
